﻿using System;
using System.Collections.Generic;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Interfaces;
using HiTeachers.Utilities;

namespace HiTeachers.Data
{
    public class StaticPageDao : AbstractNHibernateDao<StaticPage, Guid>, IStaticPageDao
    {
        public StaticPage GetRule()
        {
            try
            {
                const string hsql = "select sp from StaticPage sp where sp.ContentType = :contentType";

                var query = NHibernateSession.CreateQuery(hsql);
                query.SetParameter("contentType", ContentType.Rules);

                return query.UniqueResult<StaticPage>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return null;
            }
        }

        public StaticPage GetIntroduct()
        {
            try
            {
                const string hsql = "select sp from StaticPage sp where sp.ContentType = :contentType";

                var query = NHibernateSession.CreateQuery(hsql);
                query.SetParameter("contentType", ContentType.Intro);

                return query.UniqueResult<StaticPage>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return null;
            }
        }

        public int TotalPage(ContentType contentType)
        {
            try
            {
                const string hsql = "select count(*) from StaticPage sp where sp.ContentType = :contentType";

                var query = NHibernateSession.CreateQuery(hsql);
                query.SetParameter("contentType", contentType);

                return Convert.ToInt32(query.UniqueResult());
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return 0;
            }
        }

        public IList<StaticPage> GetStaticPages(ContentType contentType)
        {
            try
            {
                const string hsql = "select sp from StaticPage sp where sp.ContentType = :contentType order by sp.SortIndex";

                var query = NHibernateSession.CreateQuery(hsql);
                query.SetParameter("contentType", contentType);

                return query.List<StaticPage>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<StaticPage>();
            }
        }

        public IList<StaticPage> GetStaticPages(ContentType contentType, int maxResult, int startRowIndex)
        {
            try
            {
                const string hsql = "select sp from StaticPage sp where sp.ContentType = :contentType order by sp.SortIndex";

                var query = NHibernateSession.CreateQuery(hsql);
                query.SetParameter("contentType", contentType);

                query.SetMaxResults(maxResult);
                query.SetFirstResult(startRowIndex);

                return query.List<StaticPage>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<StaticPage>();
            }
        }
    }
}
